package com.xdap.keymen.service.punch;

import com.definesys.mpaas.query.MpaasQuery;
import com.xdap.keymen.entity.PunchXdapAttachmentsEntity;
import com.xdap.motor.entity.SnowflakeIdWorker;
import com.xdap.runtime.service.RuntimeAppContextService;
import com.xdap.runtime.service.RuntimeDatasourceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;


@Service
@RequiredArgsConstructor
@Slf4j
public class PunchXdapAttachmentsService extends PunchBaseService{

    private final RuntimeDatasourceService runtimeDatasourceService;
    private final RuntimeAppContextService runtimeAppContextService;
    private final SnowflakeIdWorker snowflakeIdWorker;

    private String attachmentsUrl="https://apaastest.punch.com.cn:8282/apaas/backend/test/test/xdap-app/attachments/downloadFile?file={}&token=eyJ4ZGFwdGVuYW50aWQiOiI3MzM2ODI3ODI4NjUxOTUwMDkifQ";
    public PunchXdapAttachmentsEntity getXdapAttachments(String id) {
        String currentTenantId = runtimeAppContextService.getCurrentTenantId();
        MpaasQuery query = runtimeDatasourceService.buildTenantMpaasQuery("xdap_app_" + currentTenantId);
        query.eq("id",id);
        return query.doQueryFirst(PunchXdapAttachmentsEntity.class);
    }

    public String getXdapAttachmentsUrl(String id) {
        PunchXdapAttachmentsEntity entity=getXdapAttachments(id);
        if(entity==null){
            return "";
        }
        return attachmentsUrl.replace("{}",entity.getOssObjectName());
    }
}
